//
//  Player.m
//  OurRelaxDemo
//
//  Created by 于宾尧 on 15/6/30.
//  Copyright (c) 2015年 于宾尧. All rights reserved.
//

#import "Player.h"

@implementation Player

+(instancetype)shareDataHandle
{
    static Player *playing = nil;
    static dispatch_once_t oneTaken;
    dispatch_once(&oneTaken, ^{
        
        playing = [[Player alloc]init];
        playing.playingIndex = 0;
        playing.playUrlArray = [NSMutableArray array];
        
        
    });
    return playing;
    
}

+(void)Play:(NSMutableArray *)playUrlArray
{
    [Player shareDataHandle].playUrlArray = [NSMutableArray arrayWithArray:playUrlArray];
    
    NSString *playUrl = [[Player shareDataHandle].playUrlArray objectAtIndex:[Player shareDataHandle].playingIndex];
    
    
    NSURL *url = [NSURL URLWithString:playUrl];
    
    
    [Player shareDataHandle].player = [[STKAudioPlayer alloc]init];
    
    
    [Player shareDataHandle].player.volume = 1.0f;
    [[Player shareDataHandle].player playURL:url];
    
    
}

+(void)NextPlay
{
    [[Player shareDataHandle].player stop];
    [[Player shareDataHandle].player release];
    if ([Player shareDataHandle].playingIndex < [Player shareDataHandle].playUrlArray.count) {
        
    
        NSString *playUrl = [[Player shareDataHandle].playUrlArray objectAtIndex:[Player shareDataHandle].playingIndex + 1];
    NSURL *url = [NSURL URLWithString:playUrl];
    
    [Player shareDataHandle].player = [[STKAudioPlayer alloc]init];
    
    [Player shareDataHandle].player.volume = 1.0f;
    [[Player shareDataHandle].player playURL:url];
        [Player shareDataHandle].playingIndex  = [Player shareDataHandle].playingIndex + 1;
    }
    else{
        [Player shareDataHandle].playingIndex = 0;
        
        [Player Play:[Player shareDataHandle].playUrlArray];
    }
    
}

+(void)lastPlay
{
    [[Player shareDataHandle].player stop];
    [[Player shareDataHandle].player release];
    if ([Player shareDataHandle].playingIndex > 0) {
        
        
        NSString *playUrl = [[Player shareDataHandle].playUrlArray objectAtIndex:[Player shareDataHandle].playingIndex - 1];
        NSURL *url = [NSURL URLWithString:playUrl];
        
        [Player shareDataHandle].player = [[STKAudioPlayer alloc]init];
        
        [Player shareDataHandle].player.volume = 1.0f;
        [[Player shareDataHandle].player playURL:url];
        [Player shareDataHandle].playingIndex  = [Player shareDataHandle].playingIndex - 1;
    }
    else{
        [Player shareDataHandle].playingIndex = 0;
        
        [Player Play:[Player shareDataHandle].playUrlArray];
    }
    
}

+(void)randomPlay
{

    if ([Player shareDataHandle].player.stopReason == STKAudioPlayerStopReasonPendingNext) {
        
    
    [Player shareDataHandle].playingIndex = arc4random()%([Player shareDataHandle].playUrlArray.count - 1);
    }
    NSString *playUrl = [[Player shareDataHandle].playUrlArray objectAtIndex:[Player shareDataHandle].playingIndex];
    
    
    NSURL *url = [NSURL URLWithString:playUrl];
    
    
    [Player shareDataHandle].player = [[STKAudioPlayer alloc]init];
    
    
    [Player shareDataHandle].player.volume = 1.0f;
    
    [[Player shareDataHandle].player playURL:url];
        
    
    
}

+(void)orderPlay
{
    if ([Player shareDataHandle].player.stopReason == STKAudioPlayerStopReasonPendingNext) {
        [Player NextPlay];
    }
}

+(void)roundPlay
{
    if ([Player shareDataHandle].player.stopReason == STKAudioPlayerStopReasonPendingNext) {
        NSString *playUrl = [[Player shareDataHandle].playUrlArray objectAtIndex:[Player shareDataHandle].playingIndex];
        
        
        NSURL *url = [NSURL URLWithString:playUrl];
        
        
        [Player shareDataHandle].player = [[STKAudioPlayer alloc]init];
        
        
        [Player shareDataHandle].player.volume = 1.0f;
        [[Player shareDataHandle].player playURL:url];
    }
 
}







@end
